package com.fl.nj.service.impl;

import com.fl.nj.entity.TableColumn;
import com.fl.nj.mapper.TableColumnMapper;
import com.fl.nj.service.TableColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 表字段信息Service实现类
 * 实现表字段信息的业务逻辑
 */
@Service
public class TableColumnServiceImpl implements TableColumnService {
    @Autowired
    private TableColumnMapper tableColumnMapper;

    @Override
    @Transactional
    public void save(TableColumn column) {
        // 验证字段名是否已存在
        TableColumn existingColumn = tableColumnMapper.getBySchemaIdAndColumnName(
                column.getSchemaId(), column.getColumnName());
        if (existingColumn != null) {
            throw new RuntimeException("字段名已存在: " + column.getColumnName());
        }
        
        // 验证必填字段
        validateColumn(column);
        
        // 保存表字段信息
        tableColumnMapper.save(column);
    }

    @Override
    @Transactional
    public void batchSave(List<TableColumn> columns) {
        if (columns == null || columns.isEmpty()) {
            return;
        }
        
        // 批量保存表字段信息
        tableColumnMapper.batchSave(columns);
    }

    @Override
    @Transactional
    public void update(TableColumn column) {
        // 验证字段是否存在
        TableColumn existingColumn = tableColumnMapper.getById(column.getId());
        if (existingColumn == null) {
            throw new RuntimeException("字段不存在");
        }
        
        // 验证字段名是否已被其他字段使用
        TableColumn columnWithSameName = tableColumnMapper.getBySchemaIdAndColumnName(
                column.getSchemaId(), column.getColumnName());
        if (columnWithSameName != null && !columnWithSameName.getId().equals(column.getId())) {
            throw new RuntimeException("字段名已存在: " + column.getColumnName());
        }
        
        // 验证必填字段
        validateColumn(column);
        
        // 更新表字段信息
        tableColumnMapper.update(column);
    }

    @Override
    public TableColumn getById(Long id) {
        return tableColumnMapper.getById(id);
    }

    @Override
    public List<TableColumn> getBySchemaId(Long schemaId) {
        return tableColumnMapper.getBySchemaId(schemaId);
    }

    @Override
    public TableColumn getBySchemaIdAndColumnName(Long schemaId, String columnName) {
        return tableColumnMapper.getBySchemaIdAndColumnName(schemaId, columnName);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 验证字段是否存在
        TableColumn column = tableColumnMapper.getById(id);
        if (column == null) {
            throw new RuntimeException("字段不存在");
        }
        
        // 删除表字段信息
        tableColumnMapper.delete(id);
    }

    @Override
    @Transactional
    public void deleteBySchemaId(Long schemaId) {
        // 删除指定表结构的所有字段信息
        tableColumnMapper.deleteBySchemaId(schemaId);
    }

    /**
     * 验证表字段信息的必填字段
     * @param column 表字段信息对象
     */
    private void validateColumn(TableColumn column) {
        if (column.getSchemaId() == null) {
            throw new RuntimeException("表结构不能为空");
        }
        if (column.getColumnName() == null || column.getColumnName().trim().isEmpty()) {
            throw new RuntimeException("字段名不能为空");
        }
        if (column.getColumnType() == null || column.getColumnType().trim().isEmpty()) {
            throw new RuntimeException("字段类型不能为空");
        }
        if (column.getColumnOrder() == null) {
            throw new RuntimeException("字段顺序不能为空");
        }
    }
}